#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Pick Particles                                                     #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 11/25/2016                                             #
# Last Modification: 16/10/2017                                            #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 40
#
# MANUAL: This script runs gautomatch to pick particles in the images
#
# DISPLAY: imagenumber
# DISPLAY: comment
# DISPLAY: sample_pixel
# DISPLAY: gautomatch_diameter
# DISPLAY: gautomatch_cc_cutoff
# DISPLAY: gautomatch_extractraw
# DISPLAY: gautomatch_boxsize
# DISPLAY: gautomatch_mindist
# DISPLAY: KV
# DISPLAY: CS
# DISPLAY: gautomatch_number_particles
# DISPLAY: gautomatch_extra
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set phacon = ""
set RESMIN = ""
set RESMAX = ""
set RADLIM = ""
set KV = ""
set CS = ""
set movie_stackname = ""
set import_original_time = ""
set gautomatch_diameter = ""
set gautomatch_cc_cutoff = ""
set gautomatch_number_particles = ""
set gautomatch_extra = ""
set gautomatch_extractraw = ""
set gautomatch_boxsize = ""
set gautomatch_mindist = ""
#
#$end_vars
#
set scriptname = process_gautomatch
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
echo "<<@progress: 5>>"
#
if(${import_original_time} == "-" || ${import_original_time} == "") then
  @ status_date = `date +%s` * 1000
  set date_text = "Processed at "`echo ${status_date} | awk ' { s = $1 / 1000 } END { print s } ' | ${app_gawk} '{print strftime("%c", $0)}' `
else
  set status_date = ${import_original_time}
  set date_text = "Recorded at "`echo ${status_date} | awk ' { s = $1 / 1000 } END { print s } ' | ${app_gawk} '{print strftime("%c", $0)}' `
endif
#
echo "# IMAGE: ${movie_stackname}_Sum_fft.mrc <DriftCor image FFT (2D, no DW)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${movie_stackname}.mrc <DriftCor image (2D, with DW)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${movie_stackname}_fft.mrc <DriftCor image FFT (2D, with DW)>" >> LOGS/${scriptname}.results
#

if ( ${GPU_how_many} > "1" ) then
  if ( ${GPU_cycle} == "y" ) then
    source ${proc_2dx}/2dx_find_GPU.com
    set GPU_to_use_local = ${next_GPU}
  else
    set GPU_to_use_local = 3
  endif
else 
   set GPU_to_use_local = 3
endif
#
if ( ${CS} == "ScriptWillPutNumbereHere" ) then
  set CS = ${Default_CS}
  echo "set CS = ${CS}" >> LOGS/${scriptname}.results
endif
#
if ( ${KV} == "ScriptWillPutNumbereHere" ) then
  set KV = ${Default_KV}
  echo "set KV = ${KV}" >> LOGS/${scriptname}.results
endif
#
if ( ${gautomatch_extractraw} == "y" ) then
  set extractraw = "--extract_raw"
else
  set extractraw = ""
endif
#
echo "<<@progress: 20>>"
echo "<<@evaluate>>"
#
# set min_dist = `echo ${gautomatch_diameter} | awk '{ s = 1.0 * $1 } END { print s }'`
#
if ( ! -e ${movie_stackname}.mrc ) then
  ${proc_2dx}/protest "ERROR: Drift-corrected stack ${movie_stackname}.mrc does not exist."
endif
#

##########################################################################
${proc_2dx}/linblock "Calling gautomatch"
##########################################################################
#
\rm -f ${movie_stackname}_bgfree.mrc
#
echo ":: "
echo "::Running:"
echo ":: "
echo ":: "${app_gautomatch} 
echo ":: "--apixM ${sample_pixel} 
echo ":: "--speed 1 
echo ":: "--kV ${KV}
echo ":: "--cs ${CS}
echo ":: "--diameter ${gautomatch_diameter} 
echo ":: "--cc_cutoff ${gautomatch_cc_cutoff} 
echo ":: "--write_bgfree_mic
echo ":: "--gid ${GPU_to_use_local} 
# echo ":: "--min_dist ${min_dist}
echo ":: "--min_dist ${gautomatch_mindist}
echo ":: "--boxsize ${gautomatch_boxsize}
echo ":: "${extractraw}
echo ":: "${gautomatch_extra}
echo ":: "${movie_stackname}.mrc
echo ":: "
#
${app_gautomatch} \
--apixM ${sample_pixel} \
--speed 1 \
--kV ${KV} \
--cs ${CS} \
--diameter ${gautomatch_diameter} \
--cc_cutoff ${gautomatch_cc_cutoff} \
--write_bgfree_mic \
--gid ${GPU_to_use_local} \
--min_dist ${gautomatch_mindist} \
--boxsize ${gautomatch_boxsize} \
${extractraw} \
${gautomatch_extra} \
${movie_stackname}.mrc
#
echo "#IMAGE-IMPORTANT: ${movie_stackname}_bgfree.mrc <Background-subtracted image (MRC)>" >> LOGS/${scriptname}.results
echo "#IMAGE-IMPORTANT: ${movie_stackname}_automatch.star <automatch particles (star)>" >> LOGS/${scriptname}.results
echo "#IMAGE: ${movie_stackname}_automatch.box <automatch EMAN particles (box)>" >> LOGS/${scriptname}.results
echo "#IMAGE: ${movie_stackname}_nonunique.box <nonunique particles (box)>" >> LOGS/${scriptname}.results
echo "#IMAGE: ${movie_stackname}_rejected.star <rejected particles (star)>" >> LOGS/${scriptname}.results
#
set gautomatch_number_particles = `cat ${movie_stackname}_automatch.star | wc -l | awk '{ s = $1 -9 } END { print s }'`
#
${proc_2dx}/linblock "Found ${gautomatch_number_particles} particles."
echo "set gautomatch_number_particles = ${gautomatch_number_particles}" >> LOGS/${scriptname}.results
#
#################################################################################
${proc_2dx}/linblock "Running: 2dx_mrc_converter to create a PNG file"
#################################################################################
#
echo "<<@progress: 70>>"
#
\rm -f ${movie_stackname}_bgfree_rings.png
\rm -f tmp.png
set width = `${dir_imod}/bin/clip info ${movie_stackname}.mrc | grep size | head -n 1 | cut -d\( -f2 | cut -d\, -f1`
set height = `${dir_imod}/bin/clip info ${movie_stackname}.mrc | grep size | head -n 1 | cut -d\( -f2 | cut -d\, -f2`
set xoffset = `echo ${width} ${height} | awk ' { s = ( $1 - $2 ) / 2 } END { print s }'`
# echo ":: xoffset = ${xoffset} "
#
# ${app_2dx_mrc_converter} --size ${width},${height} ${movie_stackname}_bgfree.mrc tmp.png
${app_2dx_mrc_converter} --size ${height},${height} ${movie_stackname}_bgfree.mrc tmp.png
${app_python} ${proc_2dx}/PNGdrawcircles.py tmp.png ${movie_stackname}_bgfree_rings.png ${movie_stackname}_automatch.star ${gautomatch_diameter} ${xoffset} 
\rm -f tmp.png
#
echo "#IMAGE: ${movie_stackname}_bgfree_rings.png <Background-subtracted image (labeled, PNG)>" >> LOGS/${scriptname}.results
#
if ( ${gautomatch_extractraw} == "y" ) then
  echo "#IMAGE-IMPORTANT: ${movie_stackname}_particles.mrcs <Extracted particles>" >> LOGS/${scriptname}.results
endif
#
#################################################################################
${proc_2dx}/linblock "Running: 2dx_mrc_converter to create a PNG file for inspection"
#################################################################################
#
echo "<<@progress: 80>>"
#
\rm -f ${movie_stackname}_bgfree.mrc.png
\rm -f tmp.png
\rm -f tmp2.png
\rm -f tmp3.png
${app_2dx_mrc_converter} --size 400 ${movie_stackname}_bgfree.mrc tmp.png
${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp3.png 10 350 0 "Background-free GAUTOMATCH particle image"
${app_python} ${proc_2dx}/PNGannotator.py tmp3.png ${movie_stackname}_bgfree.mrc.png 10 365 0 "${date_text}"
\rm -f tmp.png
\rm -f tmp2.png
\rm -f tmp3.png
#
echo "#IMAGE: ${movie_stackname}_bgfree.mrc.png <Background-subtracted image (annotated, PNG)>" >> LOGS/${scriptname}.results
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
exit
#
